package github.saw96x.remoting.transport.netty.client;

import github.saw96x.remoting.pojo.RpcResponse;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 服务器未处理请求
 *
 * @author Saw96x
 * @date 2022/3/18 9:03
 */
public class UnprocessedRequests {
  private static final Map<String, CompletableFuture<RpcResponse<Object>>> UNPROCESSED_RESPONSE_FUTURES = new ConcurrentHashMap<>();

  public void put(String requestId, CompletableFuture<RpcResponse<Object>> future) {
    UNPROCESSED_RESPONSE_FUTURES.put(requestId, future);
  }

  public void complete(RpcResponse<Object> rpcResponse) {
    CompletableFuture<RpcResponse<Object>> future = UNPROCESSED_RESPONSE_FUTURES.remove(rpcResponse.getRequestId());
    if (null != future) {
      future.complete(rpcResponse);
    } else {
      throw new IllegalStateException();
    }
  }
}
